package com.ebspp.server;

import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TNonblockingServerTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

import com.ebspp.global.General;
import com.ebspp.thrift.MsgTransfer;

/**
 * A stateless EBS++ server responsible for the heart beat communications
 * It is nonblocking for two reasons:
 * 1. for indicating its popularity, by the response time felt at client
 * 2. heart beat is very fast, no need for extra thread
 * 
 * @author x23pan
 *
 */
public class EBSPPMsgServer extends EBSPPServer
{

	public static void initServer() 
	{
		// TODO Auto-generated method stub
		try 
		{
			TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(General.mPort);
			MsgTransfer.Processor processor = new MsgTransfer.Processor(new MsgHandler());
			TServer server = new TNonblockingServer(
					new TNonblockingServer.Args(serverTransport).processor(processor));
			
			System.out.println("Staring message server at port: " + General.mPort);
			server.serve();
			
		} catch (TTransportException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public static void main(String[] args)
	{
		EBSPPMsgServer.initServer();
	}
}